package com.farsunset.ichat.cim.session;

import java.util.ArrayList;
import java.util.List;

import com.appleframework.cache.core.CacheManager;
import com.farsunset.cim.nio.mutual.Message;
import com.farsunset.ichat.common.util.Constants;

/**
 * 消息缓存
 *
 */
public class MessageManager {

	private CacheManager cacheManager;

	@SuppressWarnings("unchecked")
	public void setOfflineMessage(String account, Message message) {
		String key = account + Constants.OFFLINE_SUFFIX;
		List<Message> messages = (List<Message>) cacheManager.get(key);
		if (messages == null) {
			messages = new ArrayList<Message>();
		}
		messages.add(message);
		cacheManager.set(key, messages);
	}

	@SuppressWarnings("unchecked")
	public List<Message> getOfflineMessage(String account) {
		String key = account + Constants.OFFLINE_SUFFIX;
		List<Message> list = (List<Message>) cacheManager.get(key);
		if (list == null || list.size() == 0) {
			list = new ArrayList<Message>();
		}
		return list;
	}

	public void removeMessage(String account) {
		String key = account + Constants.OFFLINE_SUFFIX;
		cacheManager.remove(key);
	}

	public CacheManager getCacheManager() {
		return cacheManager;
	}

	public void setCacheManager(CacheManager cacheManager) {
		this.cacheManager = cacheManager;
	}

}
